<template>
  <div class="main-con flex1 hbox">
    <div class="vbox flex1 p8" style="border-right:10px solid #eee;">
      <div class="toolbar">
        <el-button type="primary" v-auth="'add'" plain icon="el-icon-plus" @click="addTemplate">添加模板</el-button>
      </div>
      <base-table
        ref="tempTable"
        url="template/getPage"
        code="htk"
        :param="searchParam"
        :cls="columns"
        :optionBtns="optionBtns"
        @handlerOption="handlerOption"
        @row-click="tempClick"
      ></base-table>
    </div>
    <div class="vbox flex1 p8">
      <div class="toolbar">
        <el-button
          :disabled="!searchFieldParam.tempId"
          type="primary"
          v-auth="'add_field'"
          plain
          icon="el-icon-plus"
          @click="addTempField"
        >添加模板属性</el-button>
      </div>
      <base-table
        ref="tempFieldTable"
        url="templateField/getPage"
        code="htk"
        :auto-load="false"
        :param="searchFieldParam"
        :cls="fieldColumns"
        :optionBtns="fieldOptionBtns"
        @handlerOption="fieldHandlerOption"
      ></base-table>
    </div>
    <el-dialog :visible.sync="showAddTempDialog" title="添加模板" center width="20%">
      <el-form ref="tempForm" :rules="tempRules" :model="template" label-width="95px">
        <ElFormItem label="模板名称" prop="name">
          <el-input v-model.trim="template.name" placeholder="请输入模板名称"></el-input>
        </ElFormItem>
        <ElFormItem label="模板类型" prop="type">
          <el-select
            style="width:100%"
            clearable
            v-model.trim="template.type"
            placeholder="请选择模板类型"
          >
            <el-option
              v-for="item in types"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </ElFormItem>
        <ElFormItem label="备注" prop="rmk">
          <el-input type="textarea" v-model.trim="template.rmk" placeholder="请输入备注"></el-input>
        </ElFormItem>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveTemplate">保 存</el-button>
        <el-button @click="showAddTempDialog = false">取 消</el-button>
      </span>
    </el-dialog>
    <el-dialog :visible.sync="showAddTempFieldDialog" title="添加模板属性" center width="20%">
      <el-form ref="tempFieldForm" :rules="tempFieldRules" :model="tempField" label-width="95px">
        <ElFormItem label="字段名称" prop="lable">
          <el-input v-model.trim="tempField.lable" placeholder="请输入字段名称"></el-input>
        </ElFormItem>
        <ElFormItem label="字段属性名" prop="name">
          <el-input v-model.trim="tempField.name" placeholder="请输入字段属性名"></el-input>
        </ElFormItem>
        <ElFormItem label="字段类型" prop="type">
          <el-input v-model.trim="tempField.type" placeholder="请输入字段类型"></el-input>
        </ElFormItem>
        <ElFormItem label="字段排序" prop="fieldOrder">
          <el-input-number
            :min="0"
            style="width:100%"
            v-model.trim="tempField.fieldOrder"
            placeholder="请输入字段排序"
          ></el-input-number>
        </ElFormItem>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveTempField">保 存</el-button>
        <el-button @click="showAddTempFieldDialog = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showAddTempDialog: false,
      showAddTempFieldDialog: false,
      searchParam: {},
      searchFieldParam: {
        tempId: ""
      },
      template: {
        id: "",
        name: "",
        type: "",
        rmk: ""
      },
      tempField: {
        id: "",
        lable: "",
        name: "",
        type: "",
        fieldOrder: 0
      },
      types: [
        { label: "导出", value: "order-template" },
        { label: "报关", value: "declaration-template" },
        { label: "清关", value: "clearance-template" }
      ],
      optionBtns: [
        { text: "编辑", code: "list_edit", type: "primary" },
        { text: "删除", code: "list_delete" }
      ],
      fieldOptionBtns: [
        { text: "编辑", code: "list_field_edit", type: "primary" },
        { text: "删除", code: "list_field_delete" }
      ],
      columns: [
        { prop: "name", label: "模板名称" },
        { prop: "type", label: "模板名称" },
        { prop: "rmk", label: "备注" },
        { prop: "crteTime", label: "创建时间" }
      ],
      fieldColumns: [
        { prop: "lable", label: "字段名称" },
        { prop: "name", label: "字段属性" },
        { prop: "type", label: "字段类型" },
        { prop: "fieldOrder", label: "字段排序" }
      ],
      tempRules: {
        name: [
          {
            required: true,
            message: "请输入模板名称",
            trigger: "change,blur"
          }
        ],
        type: [
          {
            required: true,
            message: "请选择模板类型",
            trigger: "change,blur"
          }
        ]
      },
      tempFieldRules: {
        lable: [
          {
            required: true,
            message: "请输入字段名称",
            trigger: "change,blur"
          }
        ]
        // type: [
        //   {
        //     required: true,
        //     message: "请选择字段类型",
        //     trigger: "change,blur"
        //   }
        // ],
        // name: [
        //   {
        //     required: true,
        //     message: "请选择字段属性名",
        //     trigger: "change,blur"
        //   }
        // ]
      }
    };
  },
  mounted() {
    this.defaultTemp = JSON.parse(JSON.stringify(this.$data.template));
    this.defaultTempField = JSON.parse(JSON.stringify(this.$data.tempField));
  },
  methods: {
    handlerOption(code, row) {
      if (code == "list_delete") {
        this.deleteTemp(row);
      } else if (code == "list_edit") {
        this.editTemp(row);
      }
    },
    fieldHandlerOption(code, row) {
      if (code == "list_field_delete") {
        this.deleteTempField(row);
      } else if (code == "list_field_edit") {
        this.editTempField(row);
      }
    },
    tempClick(row) {
      this.searchFieldParam.tempId = row.id;
      this.$refs.tempFieldTable.initLoad();
    },
    addTemplate() {
      this.showAddTempDialog = true;
      this.$nextTick(() => {
        this.$refs.tempForm.resetFields();
      });
    },
    addTempField() {
      this.showAddTempFieldDialog = true;
      this.$nextTick(() => {
        this.$refs.tempFieldForm.resetFields();
      });
    },
    search() {
      this.$refs.tempTable.initLoad();
    },
    searchField() {
      this.$refs.tempFieldTable.initLoad();
    },
    editTemp(row) {
      let tempTemp = Object.assign({}, row);
      this.showAddTempDialog = true;
      this.$nextTick(() => {
        this.template = tempTemp;
      });
    },
    editTempField(row) {
      let tempTempField = Object.assign({}, row);
      this.showAddTempFieldDialog = true;
      this.$nextTick(() => {
        this.tempField = tempTempField;
      });
    },
    deleteTemp(row) {
      let me = this;
      me.$confirm("确定要删除选中行数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        me.$http.ajax({
          url: "template/batchDelete",
          data: {
            ids: [row.id]
          },
          code: "htk",
          success: res => {
            me.$message.success("删除成功");
            me.search();
          }
        });
      });
    },
    deleteTempField(row) {
      let me = this;
      me.$confirm("确定要删除选中行数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        me.$http.ajax({
          url: "templateField/batchDelete",
          data: {
            ids: [row.id]
          },
          code: "htk",
          success: res => {
            me.$message.success("删除成功");
            me.searchField();
          }
        });
      });
    },
    saveTemplate() {
      let me = this;
      me.$refs["tempForm"].validate(valid => {
        if (valid) {
          me.$http.ajax({
            url: "template/saveTemplate",
            data: me.template,
            code: "htk",
            success: res => {
              me.$message.success("保存成功");
              me.showAddTempDialog = false;
              me.search();
            }
          });
        }
      });
    },
    saveTempField() {
      let me = this;
      me.$refs["tempFieldForm"].validate(valid => {
        if (valid) {
          me.tempField.tempId = me.searchFieldParam.tempId;
          me.$http.ajax({
            url: "templateField/saveTemplateField",
            data: me.tempField,
            code: "htk",
            success: res => {
              me.$message.success("保存成功");
              me.showAddTempFieldDialog = false;
              me.searchField();
            }
          });
        }
      });
    }
  }
};
</script>

<style>
</style>
